package com.wish.wishsystem_management.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.wishsystem_management.api.SysLog;
import com.wish.wishsystem_management.entity.SysDept;
import com.wish.wishsystem_management.entity.SysRole;
import com.wish.wishsystem_management.service.*;
import dto.systemDto.RoleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import vo.ResultEntity;

import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author fanzijun
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private ISysRoleMenuService iSysRoleMenuService;
    @Autowired
    private ISysRoleDeptService iSysRoleDeptService;
    @Autowired
    ISysDeptService iSysDeptService;

    /**
     * 角色列表
     */
    @RequestMapping("/list")
    public ResultEntity findAll(RoleDto roleDto) {
        PageHelper.startPage(roleDto.getPageNum(), roleDto.getPageSize());
        List<SysRole> list = iSysRoleService.findAll(roleDto);
        PageInfo info = new PageInfo(list);
        return ResultEntity.ok("200", "列表", info);
    }

    /**
     * 部门下角色列表
     */
    @RequestMapping("/selectBydeptId")
    public ResultEntity selectBydeptId(@RequestBody Long deptId) {
        List<SysRole> list = iSysRoleDeptService.queryListByDeptId(deptId);
        return ResultEntity.ok("200", "角色", list);
    }


    /**
     * 角色信息
     */
    @RequestMapping("/info")
    public ResultEntity info(Long roleId) {
        //根据角色id查询角色
        SysRole role = iSysRoleService.getById(roleId);
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", role.getDeptId());
        SysDept sysDept = iSysDeptService.getOne(queryWrapper);

        role.setDeptName(sysDept.getName());

        //查询角色对应的菜单
        List<Long> menuIdList = iSysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);


        return ResultEntity.ok("200", "角色查询成功", role);
    }

    /**
     * 保存角色
     */
    @SysLog("保存角色")
    @RequestMapping("/save")
    public ResultEntity save(@RequestBody RoleDto roleDto) {


        if (roleDto.getRoleId() != null) {
            iSysRoleService.updateRole(roleDto);
        } else {
            iSysRoleService.saveRole(roleDto);
        }


        return ResultEntity.ok("200", "添加成功", null);
    }

    /**
     * 删除角色
     */
    @SysLog("删除角色")
    @RequestMapping("/delete")
    public ResultEntity deleteRole(Long roleId) {
        //根据角色id查询用户
        if (iSysRoleService.queryTotalById(roleId) > 0) {
            return ResultEntity.error("401", "不能删除有用户的角色", null);
        }

        iSysRoleService.deleteRoleById(roleId);

        return ResultEntity.ok("删除");
    }


    /**
     * 修改角色
     *
     * @param roleDto
     * @return
     */

    @SysLog("修改角色")
    @RequestMapping("/update")
    public ResultEntity update(@RequestBody RoleDto roleDto) {
        iSysRoleService.updateRole(roleDto);
        return ResultEntity.ok("200", "修改角色成功", null);
    }
}

